const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu('Prefabs/BoomWithShader/BoomWithShader')
export default class BoomWithShader extends cc.Component {

    @property({ type: cc.Node, tooltip: 'center node' })
    centerNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: 'arrow list' })
    arrowList: cc.Node[] = [];

    @property({ type: cc.Camera, tooltip: 'camera' })
    camera: cc.Camera = null;

    @property({ type: cc.Sprite, tooltip: 'render sprite' })
    renderSprite: cc.Sprite = null;

    private texture: cc.RenderTexture = null;
    private _width: number = 0;
    private _height: number = 0;
    private material: cc.Material = null;

    start() {
        // this.camera.enabled = false;
        this.material = this.renderSprite.getMaterials[0];
        this.init();
    }

    onBtnPlay() {
        // x move
        // x rendertexture
        // shader


        // for (let i = 0; i < this.arrowList.length; i++) {
        //     let pos: cc.Vec3 = this.arrowList[i].position;
        //     cc.tween(this.arrowList[i])
        //         .to(0.2, { position: cc.v3(pos.x * 2, pos.y * 2, 0) })
        //         .start();
        // }
        // this.camera.enabled = true;
        let picData = this.initImage();
        this.createCanvas(picData);
        // this.camera.enabled = false;
    }

    onBtnReset() {
        for (let i = 0; i < this.arrowList.length; i++) {
            let pos: cc.Vec3 = this.arrowList[i].position;
            this.arrowList[i].position = cc.v3(pos.x / 2, pos.y / 2, 0);
        }
    }

    init() {
        let texture = new cc.RenderTexture();
        texture.initWithSize(cc.visibleRect.width, cc.visibleRect.height, cc['gfx'].RB_FMT_S8);
        this.camera.targetTexture = texture;
        this.texture = texture;
    }

    initImage() {
        let data = this.texture.readPixels();
        this._width = this.texture.width;
        this._height = this.texture.height;
        let picData = this.filpYImage(data, this._width, this._height);
        return picData;
    }

    createCanvas(picData) {
        let texture = new cc.Texture2D();
        texture.initWithData(picData, 32, this._width, this._height);

        this.renderSprite.spriteFrame = new cc.SpriteFrame(texture);
        cc.resources.load('shader/shaking/ha-shaking', cc.Material, (err: Error, res: cc.Material) => {
            if (err) { cc.error(err); return; }

            // this.renderSprite.setMaterial(0, cc.Material.getBuiltinMaterial('2d-gray-sprite'));
            this.renderSprite.setMaterial(0, res);
            // this.renderSprite.getMaterial(0).setProperty('texture', texture);
        })

        let node = this.renderSprite.node;

        node.on(cc.Node.EventType.TOUCH_MOVE, (event) => {
            let endPoint = event.getLocation();
            let point = node.parent.convertToNodeSpaceAR(endPoint);
            node.position = new cc.Vec3(point.x, point.y, 0);
        });

        // let spriteFrame = new cc.SpriteFrame();
        // spriteFrame.setTexture(texture);

        // let node = new cc.Node();
        // let sprite = node.addComponent(cc.Sprite);
        // sprite.spriteFrame = spriteFrame;

        // node.zIndex = cc.macro.MAX_ZINDEX;
        // node.parent = cc.director.getScene();
        // // set position
        // let width = cc.winSize.width;
        // let height = cc.winSize.height;
        // node.x = width / 2;
        // node.y = height / 2;
        // node.on(cc.Node.EventType.TOUCH_MOVE, (event) => {
        //     let endPoint = event.getLocation();
        //     let point = node.parent.convertToNodeSpaceAR(endPoint);
        //     node.position = new cc.Vec3(point.x, point.y, 0);
        // });

        // this.captureAction(node, width, height);
    }

    captureAction(capture, width, height) {
        let scaleAction = cc.scaleTo(1, 0.3);
        let targetPos = cc.v2(width - width / 6, height / 4);
        let moveAction = cc.moveTo(1, targetPos);
        let spawn = cc.spawn(scaleAction, moveAction);
        capture.runAction(spawn);
        let blinkAction = cc.blink(0.1, 1);
        // scene action
        this.node.runAction(blinkAction);
    }

    filpYImage(data, width, height) {
        let picData = new Uint8Array(width * height * 4);
        let rowBytes = width * 4;
        for (let row = 0; row < height; row++) {
            let srow = height - 1 - row;
            let start = srow * width * 4;
            let reStart = row * width * 4;
            // save the piexls data
            for (let i = 0; i < rowBytes; i++) {
                picData[reStart + i] = data[start + i];
            }
        }
        return picData;
    }
}
